﻿using System.Collections;
using System.Collections.Generic;
using LitJson;
using PathologicalGames;
using UnityEngine;



public class SkillPoolManager : MonoBehaviour
{
    public static SkillPoolManager _instance;

    public SpawnPool SkillPool;
    public Transform[] SkillPrefabs;
    
    private Dictionary<ushort,Transform> SkillEffectPrefabDic  = new Dictionary<ushort, Transform>();
    private Dictionary<ushort,SkillConfigInfo> SkillConfigInfoDic = new Dictionary<ushort, SkillConfigInfo>(); 

	// Use this for initialization
	void Awake ()
	{
	    _instance = this;
        //todo 解析技能Json配置表
	    TranslateSkillJsonlConfig();
        SkillPool =PoolManager.Pools.Create("SkillPool");
	    SkillPool.group.parent = transform;
        //SkillPool = PoolManager.Pools["SkillPool"];
        for (int i = 0; i < SkillPrefabs.Length; i++)
        {
            string prefabID = SkillPrefabs[i].name.Substring(0,3); 
            //将预设添加到字典内方便查找
            SkillEffectPrefabDic.Add(ushort.Parse(prefabID), SkillPrefabs[i]);
            //将预设添加到池内
            PrefabPool prefabPool = new PrefabPool(SkillPrefabs[i]);
            //todo 同时根据ID,找到相关技能配置信息添加不同的脚本上去，添加脚本到特效上
            
            prefabPool.preloadAmount = 5;
            prefabPool.cullDespawned = true;
            prefabPool.cullAbove = 5;
            prefabPool.cullDelay = 5;
            prefabPool.cullMaxPerPass = 1;
            SkillPool.CreatePrefabPool(prefabPool);
            print("添加特效ID:"+prefabID);
        }
	}

    /// <summary>
    /// 解析技能配置信息
    /// </summary>
    public void TranslateSkillJsonlConfig()
    {
        TextAsset skillText = Resources.Load<TextAsset>("SkillConfigInfos");
        SkillConfigInfo[] infos = JsonMapper.ToObject<SkillConfigInfo[]>(skillText.text);
        for (int i = 0; i < infos.Length; i++)
        {
            SkillConfigInfoDic.Add(infos[i].SkillID,infos[i]);
        }
    }

    public void DespawnInstance(Transform skillInstance)
    {
        SkillPool.Despawn(skillInstance);
    }

    /// <summary>
    /// 生成OnceShotSkill
    /// </summary>
    /// <param name="SkillID"></param>
    public void SpawnNewInstance(ushort SkillID)
    {
        Transform prefab = SkillEffectPrefabDic[SkillID];
        SkillPool.Spawn(prefab);
    }

    // Update is called once per frame
	void Update () {
		
	}
}
